package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Product;
import com.kuang.pojo.supplier;
import com.kuang.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
@Qualifier("productservice")
private ProductServiceImpl productService;

@RequestMapping("/getProduct")
public String  getProduct(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize,Model model){
    PageHelper.startPage(pageNum,pageSize);
    List<Product> product = productService.getProduct();

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    
 PageInfo<Product> productPageInfo=new PageInfo<>(product);

        if (pageNum==0 ||pageNum>productPageInfo.getPages()){

        return "redirect:/product/getProduct";

    }
        model.addAttribute("productPageInfo",productPageInfo);
        return "product";
}
@RequestMapping("/redirctadd")
public String redictadd(){


    return "addproduct";
}
@RequestMapping("/addproduct")
public String addProduct(Product product){
    productService.addProduct(product);
    return  "redirect:/product/getProduct";
}

@RequestMapping("/redirectupdateproduct")
public String redictUpdate(int id,Model model){

    Product product = productService.getProductById(id);
    model.addAttribute("product",product);
    return "updateProduct";
}


@RequestMapping("/updateProduct")
    public String updateProduct(Product product){
    productService.updateProduct(product);
    return "redirect:/product/getProduct";
}
@RequestMapping("/queryProductbyname")
 public String   queryProductByName(String name,Model model){
    List<Product> products = productService.queryProductByName(name);
    model.addAttribute("products",products);
    return "bynameproduct";
}
@RequestMapping("deleteProduct")
public String deleteProduct(int id){
    productService.deleteProduct(id);
 return "redirect:/product/getProduct";
}
@RequestMapping("/getProductById")
public String getProductById(int id,Model model){
    Product productById = productService.getProductById(id);
    model.addAttribute("product",productById);
    return "byidgetprouct";
}
//

}
